import BaseThreeScene from './scene.js';
import settings from './settings.js';
import * as THREE from 'three';
import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';

import * as streaSTT from '../utils/streaSTT.js'




export default function  threeScene() 
{

  let setting = new settings({
    modelUrl:'/models/wumen2.glb',
    hdrURl:'/textures/venice_sunset_1k.hdr',
    hdr_rotation:{x:0,y:-Math.PI/3,z:0},
    debug:false,
    glowFactor:20,
    isLoadModel:true,
    animations:[
      {"name":"idle","type":"idle","url":"/models/animations/mixamo/Idle.fbx","autoPlay":true},
      {"name":"说话1","type":"Talk","url":"/models/animations/mixamo/Talking.fbx","autoPlay":false},
    ]
  });
  // 动画类型 要统一设定  

  let Wolf3D_Head; // Head


const corresponding = 
{
  A: "viseme_PP",
  B: "viseme_kk",
  C: "viseme_I",
  D: "viseme_AA",
  E: "viseme_O",
  F: "viseme_U",
  G: "viseme_FF",
  H: "viseme_TH",
  X: "viseme_PP"
}

// const corresponding = 
// {
//   viseme_CH : 6,
//   viseme_DD : 4,
//   viseme_E: 11,
//   viseme_FF: 2,
//   viseme_I: 12,
//   viseme_O: 13,
//   viseme_PP: 1,
//   viseme_RR: 9,
//   viseme_SS: 7,
//   viseme_TH:3,
//   viseme_U:14,
//   viseme_aa:10,
//   viseme_kk:5,
//   viseme_nn:8,
//   viseme_sil:0
// }


// let syncData = [
//   { "start": 0.00, "end": 0.15, "value": "B" },
//   { "start": 0.15, "end": 0.29, "value": "E" },
// ]


  function onclick(mesh) {
    
  }  


  function onFinish(model) 
  {
    Wolf3D_Head = model.getObjectByName("Wolf3D_Head");
    const Wolf3D_Body = model.getObjectByName("Wolf3D_Body");
    const body_material = Wolf3D_Body.material
    console.log(Wolf3D_Head);

    
    // Wolf3D_Head.morphTargetInfluences[0]
  } 



    
  function bodyAnimation(action)
  {
    if(!Wolf3D_Head) return false;
    console.log("对应执行肢体动作:"+action)
    threeScene.changePlayerAnimation(action);
  }

  function LipSyncAnimation(SyncValue)
  {
    if(!Wolf3D_Head) return false;
    // 每次初始化
    Object.values(corresponding).forEach((value)=> {
      Wolf3D_Head.morphTargetInfluences[Wolf3D_Head.morphTargetDictionary[value]] = 0
    })

    if(SyncValue)
    {
      let syncTarget = corresponding[SyncValue];
      Wolf3D_Head.morphTargetInfluences[Wolf3D_Head.morphTargetDictionary[syncTarget]] = 1;
    }
  }


  function onprogress(xhr) {
    let loadNum = (xhr.loaded / xhr.total * 100).toFixed(2) ;
    console.log( loadNum+ '% loaded' );
  }

  function loadScene(threeDom) 
  {
    threeScene = new BaseThreeScene(threeDom,onFinish,onprogress,onclick,setting)
  }



  return {
    loadScene,
    LipSyncAnimation,
    bodyAnimation
  }
}